<template>
    <el-dialog :title="'修改'" :close-on-click-modal="false" :visible.sync="visible" @closed="dialogClose">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
            <!--       @keyup.enter.native="dataFormSubmit()" -->
            <el-form-item label="库存" prop="stock">
                <!-- <el-input v-model="dataForm.stock" placeholder="库存"></el-input> -->
                <el-input-number style="width:160px" v-model="dataForm.stock" :min="0"></el-input-number>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    export default {
        data() {
            return {
                visible: false,
                dataForm: {
                    stock: 0,
                    skuId: '',
                },
                attrGroups: [],
                dataRule: {},
                form: {},
            };
        },
        methods: {
            init(id) {
                this.dataForm.skuId = id || 0;
                this.visible = true;

                this.$nextTick(() => {
                    this.$refs["dataForm"].resetFields();
                    if (this.dataForm.skuId) {
                        this.$http({
                            url: this.$http.adornUrl(
                                `/product/skuinfo/info/${this.dataForm.skuId}`
                            ),
                            method: "get",
                            params: this.$http.adornParams({
                                shopId: sessionStorage.getItem('shopId')
                            })
                        }).then(({ data }) => {
                            if (data && data.code === 0) {
                                console.log(data);
                                this.dataForm.stock = data.skuInfo.stock;

                            }
                        });
                    }
                });
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs["dataForm"].validate(valid => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(
                                `/product/skuinfo/${!this.dataForm.skuId ? "save" : "update"}`
                            ),
                            method: "post",
                            data: this.$http.adornData({
                                skuId: this.dataForm.skuId,
                                stock: this.dataForm.stock,
                            })
                        }).then(({ data }) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: "操作成功",
                                    type: "success",
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false;
                                        this.$emit("refreshDataList");
                                    }
                                });
                            } else {
                                this.$message.error(data.msg);
                            }
                        });
                    }
                });
            },
            //dialogClose
            dialogClose() {
                this.catelogPath = [];
            }
        }
    };
</script>